package com.shangyueshang.response;

import lombok.Getter;
import org.springframework.http.HttpStatus;

/**
 * @author: JokeyZheng
 * @email: zhengjingfeng@ruqimobility.com
 * @created: 2022/1/5 17:19
 * @version: v1.0.0
 */
public class HttpResult<T> {
	
	@Getter
	private int code = 200;
	
	@Getter
	private String message = "成功";
	
	@Getter
	private T data;
	
	public static <T> HttpResult<T> fail(int code, String message) {
		HttpResult<T> httpResult = new HttpResult<>();
		return httpResult.setCode(code).setMessage(message);
	}
	
	public static <T> HttpResult<T> fail(String message) {
		return fail(HttpStatus.INTERNAL_SERVER_ERROR.value(), message);
	}
	
	public static <T> HttpResult<T> error() {
		return fail(HttpStatus.INTERNAL_SERVER_ERROR.value(), "未知异常，请联系管理员");
	}
	
	public static <T> HttpResult<T> ok() {
		return new HttpResult<>();
	}
	
	public static <T> HttpResult<T> ok(T data) {
		HttpResult<T> httpResult = new HttpResult<>();
		return httpResult.setData(data);
	}
	
	private HttpResult<T> setCode(int code) {
		this.code = code;
		return this;
	}
	
	private HttpResult<T> setMessage(String message) {
		this.message = message;
		return this;
	}
	
	private HttpResult<T> setData(T data) {
		this.data = data;
		return this;
	}
}
